package com.yld.car.market;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.yld.car.common.ConstantUtils;
import com.yld.car.common.NetworkProgressUtils;
import com.yld.car.domain.BaseColorInfo;

/**
 * 配置页面，分为两个板块，套装，车配置
 * 
 * @author apple
 * 
 */
public class FindCarFilterConfigurationActivity extends BaseActivity {

	private Button rightButton;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.find_car_filter_config_info);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);

		this.settingTitle(R.id.title, "热门配置");
		// showBackButton();

		list1 = (ListView) findViewById(R.id.list1);

		list1.setOnItemClickListener(list1ItemClickListener);
		mBar = (ProgressBar) findViewById(R.id.progressBar);
		rightButton = (Button) findViewById(R.id.rightButton);

		this.rightButton.setVisibility(View.VISIBLE);
		this.rightButton.setText("确定");
		this.rightButton.setOnClickListener(completeListener);
		utils = NetworkProgressUtils.getInstance();

		Intent intent = getIntent();
		cId = intent.getStringExtra("cId");
		// System.out.println("cId=====" + cId);

		new ConfigurationTask().execute("");
	}

	private String cId;
	private ListView list1;
	private NetworkProgressUtils utils;
	private ProgressBar mBar;
	private long[] mCheckIds;
	ArrayList<BaseColorInfo> all = null;
	private View.OnClickListener completeListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {

			StringBuffer stringBuffer = new StringBuffer();
			if (all != null && all.size() > 0&&mCheckIds!=null) {

				for (int i = 0; i < mCheckIds.length; i++) {

					String name = all.get((int) mCheckIds[i]).getName();
					stringBuffer.append(name).append(";");
				}

			}
			// System.out.println(stringBuffer.toString());

			mApp.setFilterConfigurationValue(stringBuffer.toString());
			finish();
		}
	};
	private OnItemClickListener list1ItemClickListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> adapterView, View view,
				int position, long id) {

			// mApp.setConfigurationOneIds(list1.getCheckItemIds());
			// mCheckOneIds = list1.getCheckItemIds();
			mCheckIds = list1.getCheckItemIds();

			for (int i = 0; i < mCheckIds.length; i++) {

				System.out.println("id===" + mCheckIds[i]);
			}
			// System.out.println(view);
			// System.out.println(view.findViewById(android.R.id.toggle));
			// System.out.println(l);

		}
	};

	private class ConfigurationTask extends
			AsyncTask<String, String, ArrayList<BaseColorInfo>> {

		public ConfigurationTask() {
		}

		@Override
		protected void onPreExecute() {

			mBar.setVisibility(View.VISIBLE);
			list1.setVisibility(View.GONE);
			super.onPreExecute();
		}

		@Override
		protected ArrayList<BaseColorInfo> doInBackground(String... params) {

			HashMap<String, String> allParams = new HashMap<String, String>();
			allParams.put("classId", cId);

			Object o = utils.getWebservicesByData(
					ConstantUtils.NAME_SPACE,
					ConstantUtils.ALL_METHROD_NAME.get(33),
					ConstantUtils.NAME_SPACE
							+ ConstantUtils.ALL_METHROD_NAME.get(33),
					ConstantUtils.GET_CAR_DEPLOY_BY_CLASS_ID_URL, allParams);

			all = utils.parseFindByFilterColor(o.toString(), "table");

			return all;
		}

		@Override
		protected void onPostExecute(ArrayList<BaseColorInfo> result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);

			mBar.setVisibility(View.GONE);
			list1.setVisibility(View.VISIBLE);
			ArrayList<String> all = null;
			if (result != null) {

				all = new ArrayList<String>();
				for (BaseColorInfo b : result) {

					all.add(b.getName());
				}
				MyConfigurationAdapter adapter = new MyConfigurationAdapter(all);

				// ArrayAdapter<String> adapter = new ArrayAdapter<String>(
				// getApplicationContext(),
				// // android.R.layout.simple_list_item_multiple_choice,
				// android.R.id.text1, all);
				list1.setAdapter(adapter);
				list1.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
			}
		}
	}

	private class MyConfigurationAdapter extends BaseAdapter {

		private List<String> listDatas;

		public MyConfigurationAdapter(List<String> listDatas) {
			this.listDatas = listDatas;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return listDatas.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return listDatas.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			convertView = LayoutInflater.from(getApplicationContext()).inflate(
					android.R.layout.simple_list_item_multiple_choice, null);
			//convertView.setMinimumHeight(160);
			TextView tv = (TextView) convertView
					.findViewById(android.R.id.text1);
			tv.setTextColor(getBaseContext().getResources().getColor(
					R.color.black_light));
			tv.setText(listDatas.get(position));
			return convertView;
		}

	}
}
